var t = require("../../api.js"), app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:'',
    sms: '',
    showSms:true,
    timeout: '',
    page:'',
    goodsId: '',
    userImg: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      goodsId:options.id,
      page:options.start
    })
wx.getStorage({
      key: 'user_info',
      success: (result)=>{
        console.log(result)
        if (result.data) {
          this.setData({
            userImg: result.data.avatar_url
          })
        }
        
      },
      fail: ()=>{},
      complete: ()=>{}
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  bindphone(obj) {
    this.setData({
      phone:obj.detail.value
    })
  },
  bindsms(obj) { 
    this.setData({
      sms: obj.detail.value
    })
  },
//获取验证码
  getSms() {
    if (!this.data.phone) return wx.showToast({ title: '手机号不能为空!',icon:'none'});
    if (!(/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[03678]|18[0-9]|14[57])[0-9]{8}$/).test(this.data.phone)) return wx.showToast({ title: '手机号格式错误!',icon:'none'});
    var total = 120;
    this.setData({
      showSms:false
    })
    var getCode = setInterval(() => {
      total--;
      this.setData({
        timeout: total + '秒后重新获取'
      })
      if (total === 0) {
        clearInterval(getCode)
        this.setData({
          showSms: true
        })
      }
    }, 1000);
    app.request({
      url: t.default.sendSms + '&mobile=' + this.data.phone,
      method: "get",
      success: (result) => {
        wx.showToast({
          title: result.msg,
          duration: 1500,
        });
        console.log(result)
      }
    })
  },
//提交
  submit() {
    app.request({
      url: t.default.mobile + '&mobile=' + this.data.phone +'&code='+this.data.sms,
      method: "get",
      success: (result) => {
        console.log(result)
        if (result.code == 0) {
          if (this.data.goodsId) {
            wx.redirectTo({
              url:'/pages/goods/goods?id='+this.data.goodsId
            })
          } else {
            wx.redirectTo({
              url: '/pages/' + this.data.page + '/' + this.data.page,
            });
          }
        } else {
          wx.showToast({
            title: result.msg,
            duration:1500
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})